#include <cstdio>
#include <algorithm>

using namespace std;

#define N 101

int tree[N];

int find_root(int x);

class graph_edge {
public:
    int a, b;
    int cost;

    bool operator<(const graph_edge &e) const {
        return cost < e.cost;
    }
} edge[6000];

/*
int main(int argc, char const *argv[]) {
    int n;
    while (scanf("%d", &n) != EOF && n != 0) {
        int state;
        for (int i = 0; i < n * (n - 1) / 2; ++i){
            scanf("%d%d%d%d", &edge[i].a, &edge[i].b, &edge[i].cost, &state);
            if (state == 1)
                edge[i].cost = 0;
        }
        sort(edge, edge + n * (n - 1) / 2);
        for (int i = 1; i <= n; ++i)
            tree[i] = -1;
        int result = 0;

        for (int i = 0; i < n * (n - 1) / 2; ++i) {
            int a_inedx = find_root(edge[i].a);
            int b_index = find_root(edge[i].b);
            if (a_inedx != b_index) {
                tree[a_inedx] = b_index;
                result += edge[i].cost;
            }
        }
        printf("%d\n", result);
    }
    return 0;
}
*/
// 2018.5.28 unsolved problem 
// 2018.5.29 solved
int main(int argc, char const *argv[]) {
    int n;
    while (scanf("%d", &n) != EOF && n != 0) {
        int bloaked_road_count = 0;
        for (int i = 1; i <= n; ++i)
            tree[i] = -1;
        for (int i = 0; i < n * (n - 1) / 2; ++i){
            int temp_a, temp_b, temp_cost, temp_state;
            scanf("%d%d%d%d", &temp_a, &temp_b, &temp_cost, &temp_state);
            if (temp_state == 1){
                // maybe circle without the following judgement
                // 2018.5.29 fix
                int a_index = find_root(temp_a);
                int b_index = find_root(temp_b);
                if (a_index != b_index)
                    tree[a_index] = b_index;
                /*
                error as follow:
                if (find_root(temp_a) != find_root(temp_b))
                    tree[temp_a] = temp_b; 
                //must change the root's pointer, not leave's pointer
                */
            }
            else{
                edge[bloaked_road_count].a = temp_a;
                edge[bloaked_road_count].b = temp_b;
                edge[bloaked_road_count++].cost = temp_cost;
            }
        }
        sort(edge, edge + bloaked_road_count);

        int result = 0;
        for (int i = 0; i < bloaked_road_count; ++i) {
            int a_inedx = find_root(edge[i].a);
            int b_index = find_root(edge[i].b);
            if (a_inedx != b_index) {
                tree[a_inedx] = b_index;
                result += edge[i].cost;
            }
        }
        printf("%d\n", result);
    }
    return 0;
}

int find_root(int x) {
    if (tree[x] == -1)
        return x;
    else {
        int temp_index = find_root(tree[x]);
        tree[x] = temp_index;
        return temp_index;
    }
}
